package 比屋作业.day15;
import java.util.*;
public class D15_2 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        Random random=new Random();
        String []choices={"石头","剪刀","布"};
        System.out.println("欢迎来到剪刀石头布游戏！");
        String computerchoice=choices[random.nextInt(3)];
        String Userchoice=getUserchoice(scanner);
        System.out.println("计算机选择了："+computerchoice);
        System.out.println("你选择了："+Userchoice);
        String result=decideWinner(computerchoice,Userchoice);
        System.out.println("结果是："+result);
    }
    private static String getUserchoice(Scanner scanner){
        String []choices={"剪刀","石头","布"};
        String Userchoice;
        while(true){
            System.out.println("请输入你的选择：");
            Userchoice=scanner.nextLine();
            for(String choice:choices){
                if (choice.equals(Userchoice)){
                    return Userchoice;
                }
            }
            System.out.println("输入无效，请重新输入！");
        }
    }
    private static String decideWinner(String computerchoice,String Userchoice){
        if (computerchoice.equals(Userchoice)){
            return "平局";
        }
        else if(computerchoice.equals("石头")&&Userchoice.equals("剪刀")||
                computerchoice.equals("剪刀")&&Userchoice.equals("布")||
        computerchoice.equals("布")&&Userchoice.equals("石头")) {
            return "你输了!";
        }
        else {
            return "你赢了！";
        }
    }
}
